﻿using System.Collections.Generic;
using System.Text;
using System;
using System.Linq;

namespace Commons.Models.Settings.Matchers
{
	[Serializable]
	public class AndMatcher<T> : IMatcher<T>
	{
		public List<IRule<T>> Rules {get; private set;}

		public AndMatcher()
			: this(new List<IRule<T>>())
		{
		}

		public AndMatcher(List<IRule<T>> rules)
		{
			this.Rules = rules;
		}

		/// <summary>
		/// Evaluates if the Object passed as parameter complies with all associated rules.
		/// Returns true only if you meet all the rules.
		/// Performs a "and" between all the rules.
		/// </summary>
		/// <param name="target">target object to evaluate</param>
		/// <returns>true only if you meet the alls rules.</returns>
		public bool Met(T target)
		{
			if (target == null)
			{
				return false;
			}

			foreach (IRule<T> rule in Rules)
			{
				if (!rule.Met(target))
				{
					return false;
				}
			}

			return true;
		}

		public void AddRule(IRule<T> rule)
		{
			Rules.Add(rule);
		}

		public override string ToString()
		{
			return string.Format("[AndMatcher {0}]", string.Join(";", Rules.Select(rule => rule.ToString()).ToArray()));
		}
	}
}
